<?php
/**
 * 时间转换为人性化的时间显示，刚刚、几秒前……
 * @param string | int $time 时间戳或符合时间格式的字符串
 * @param string $local 语言 zh_CN en
 * @return string
 */
function humanizer_datetime($time, $local = 'zh_CN')
{
    try {
        $from = new DateTime();
        if (!is_numeric($time)) {
            $time = strtotime($time);
        }
        $from->setTimestamp($time);
        return Coduo\PHPHumanizer\DateTimeHumanizer::difference(new DateTime(), $from, $local);
    } catch (Exception $e) {
        return '';
    }
}

//dump_pre(humanizer_datetime("2020-4-5T23:00"));

//人民币元到分
function y2f($rmb)
{
    $rmb = floor($rmb * 10 * 10);
    return $rmb;
}

//人民币分到元
function f2y($rmb, $round = 'float')
{
    $rmb = floor($rmb * 100) / 10000;
    if ($round == 'float') {
        $rmb = number_format($rmb, 2, '.', '');
    } elseif ($round == 'round') {
        $rmb = round($rmb);
    } elseif ($round == 'ceil') {
        $rmb = ceil($rmb);
    } elseif ($round == 'floor') {
        $rmb = floor($rmb);
    }
    return $rmb;
}


/**
 * 把驼峰命名法转换成下划线拼接命名法
 * @param string $str
 * @return string
 */
function camecase_to_underline($str)
{
    $temp_array = array();
    for ($i = 0; $i < strlen($str); $i++) {
        $ascii_code = ord($str[$i]);
        if ($ascii_code >= 65 && $ascii_code <= 90) {
            if ($i == 0) {
                $temp_array[] = chr($ascii_code + 32);
            } else {
                $temp_array[] = '_' . chr($ascii_code + 32);
            }
        } else {
            $temp_array[] = $str[$i];
        }
    }
    return implode('', $temp_array);

}

/**
 * 把下划线拼接命名法转换成驼峰命名法
 * @param string $str
 * @param boolean $ucfirst
 * @return string
 */
function underline_to_camecase($str, $ucfirst = true)
{
    $str = explode('_', $str);

    foreach ($str as $key => $val)
        $str[$key] = ucfirst($val);

    if (!$ucfirst)
        $str[0] = strtolower($str[0]);

    return implode('', $str);
}


/**
 * 字节格式化 把字节数格式为 B K M G T 描述的大小
 * @param $size
 * @param int $dec
 * @return string
 */
function byte_format($size, $dec = 2)
{
    $unit = array("B", "KB", "MB", "GB", "TB", "PB");
    $pos = 0;
    while ($size >= 1024) {
        $size /= 1024;
        $pos++;
    }
    return round($size, $dec) . " " . $unit[$pos];
}

/**
 * 从1M还原回数字1024，最小byte为单位
 * @param int $size
 * @return float|int
 */
function real_size_byte($size = 0)
{
    if (!$size) {
        return 0;
    }
    $scan = array();
    $scan['pb'] = 1024 * 1024 * 1024 * 1024 * 1024; //1024 * 1024 * 1024 * 1024 * 1024;
    $scan['p'] = $scan['pb']; //1024 * 1024 * 1024 * 1024 * 1024;
    $scan['tb'] = 1024 * 1024 * 1024 * 1024; //1024 * 1024 * 1024 * 1024;
    $scan['t'] = $scan['tb'];
    $scan['gb'] = 1073741824; //1024 * 1024 * 1024;
    $scan['g'] = $scan['gb'];
    $scan['mb'] = 1048576; //1024 * 1024;
    $scan['m'] = $scan['mb'];
    $scan['kb'] = 1024;
    $scan['k'] = $scan['kb'];
    $scan['b'] = 1;

    foreach ($scan as $unit => $factor) {
        if (strlen($size) > strlen($unit)
            && strtolower(substr($size, strlen($size) - strlen($unit))) == $unit) {
            return substr($size, 0, strlen($size) - strlen($unit)) * $factor;
        }
    }

    return $size;
}
